package com.gscitysfy.cus.drain.modules.pipe.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gscitysfy.cloudfrmwk.assistant.domain.Result;
import com.gscitysfy.cus.drain.annotations.DictTranslation;
import com.gscitysfy.cus.drain.annotations.FieldParam;
import com.gscitysfy.cus.drain.constants.DicConstant;
import com.gscitysfy.cus.drain.modules.pipe.entity.DrainPipeNetworkJob;
import com.gscitysfy.cus.drain.modules.pipe.entity.DrainPipeNetworkPage;
import com.gscitysfy.cus.drain.modules.pipe.entity.DrainPipeNetworkPlan;
import com.gscitysfy.cus.drain.modules.pipe.service.IDrainPipeNetworkJobService;
import com.gscitysfy.cus.drain.modules.pipe.service.IDrainPipeNetworkPlanService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author admin
 * @since 2022-11-15 10:49:35
 */
@RestController
@RequestMapping("/pipe/plan")
@Slf4j
@Tag(name = "管网检测管理-》检测计划管理")
public class DrainPipeNetworkPlanController {

    @Resource
    private IDrainPipeNetworkPlanService planService;

    @Resource
    private IDrainPipeNetworkJobService jobService;

    @Resource
    private HttpServletResponse response;

    /**
     * 获取监测计划列表
     * @param plan
     * @return
     */
    @Operation(summary = "获取监测计划列表")
    @PostMapping("/list")
    @Parameters({
            @Parameter(required = false, name = "cmPlanPipeType", description = "管网类型"), @Parameter(required = false, name = "cmPlanFinishStatus", description = "完成状态"),
            @Parameter(required = false, name = "cmPlanAuditStatus", description = "审核状态"), @Parameter(required = false, name = "cmPlanNo", description = "项目编号"),
            @Parameter(required = false, name = "cmPlanStartDate", description = "项目周期开始时间"),@Parameter(required = false, name = "cmPlanName", description = "项目名称"),
            @Parameter(required = false, name = "size", description = "分页每页条数"), @Parameter(required = true, name = "current", description = "当前页"),
    })
    @DictTranslation({
            @FieldParam(dictType = DicConstant.PLAN_PIPE_TYPE, targetField = "cmPlanPipeType")
    })
    public Result<IPage<DrainPipeNetworkPlan>> list(@RequestBody DrainPipeNetworkPlan plan){
        Page<DrainPipeNetworkPlan> page = new Page<>();
        page.setSize(plan.getSize()); page.setCurrent(page.getCurrent());
        return planService.findPlanList(page, plan);
    }


    /**
     * 保存或者修改监测计划
     * @return
     */
    @Operation(summary = "保存或者修改监测计划")
    @PostMapping("/save")
    @Parameters({
            @Parameter(required = true, name = "cmPlanPipeType", description = "管网类型"), @Parameter(required = true, name = "cmPlanName", description = "项目名称"),
            @Parameter(required = true, name = "cmPlanStartDate", description = "项目周期开始时间"), @Parameter(required = true, name = "cmPlanEndDate", description = "项目周期结束时间"),
            @Parameter(required = true, name = "cmPlanRangeDescription", description = "检测范围描述"),
            @Parameter(required = true, name = "pipList", description = "对象数组(对象属性包含管道编号，管道所在道路，经纬度)，形如[{'cmPipeNo':'123456','cmPipRoad':'贵池路','cmPipeLng':'123.33232','cmPipeLat':'23.1223'}]")
    })
    public Result<Boolean> save(@RequestBody DrainPipeNetworkPlan plan){
        return planService.andOrUpdate(plan);
    }

    /**
     * 获取点位相关信息（补齐管线所属道路信息）
     * @return
     */
    @Operation(summary = "获取点位相关信息（补齐管线所属道路信息）")
    @PostMapping("/find-pip")
    @Parameters({
            @Parameter(required = true, name = "pipList", description = "对象数组(对象属性包含管道编号，管道所在道路，经纬度)，形如[{'cmPipeNo':'123456','cmPipeLng':'123.33232','cmPipeLat':'23.1223'}]")
    })
    public Result<List<DrainPipeNetworkPage>> findPip(@RequestBody DrainPipeNetworkPlan plan){
        return planService.findPip(plan);
    }


    /**
     * 删除监测计划
     * @return
     */
    @Operation(summary = "删除监测计划")
    @GetMapping("/del-plan")
    @Parameters({
            @Parameter(required = true, name = "ids", description = "计划id，多个用逗号分隔")
    })
    public Result<Boolean> del(@RequestParam("ids") String ids){
        return planService.del(ids);
    }

    /**
     * 审核监测计划
     * @return
     */
    @Operation(summary = "审核监测计划")
    @PostMapping("/audit")
    @Parameters({
            @Parameter(required = true, name = "cmKid", description = "计划id"), @Parameter(required = true, name = "cmPlanAuditStatus", description = "审核状态"),
            @Parameter(required = true, name = "cmPlanAuditOpinion", description = "审核意见")
    })
    public Result<Boolean> audit(@RequestBody DrainPipeNetworkPlan plan){
        return planService.audit(plan);
    }


    /**
     * 派发检测任务
     * @return
     */
    @Operation(summary = "派发检测任务")
    @PostMapping("/create-job")
    @Parameters({
            @Parameter(required = true, name = "cmPlanId", description = "计划id"), @Parameter(required = true, name = "cmJobPipeType", description = "管网编号"),
            @Parameter(required = true, name = "cmJobName", description = "任务名称"), @Parameter(required = true, name = "cmJobStartDate", description = "任务计划开始时间"),
            @Parameter(required = true, name = "cmPlanNo", description = "计划编号"), @Parameter(required = true, name = "cmJobUtil", description = "检测单位"),
            @Parameter(required = true, name = "cmJobPeople", description = "检测人员名称"), @Parameter(required = true, name = "cmJobPeopleId", description = "检测人员id"),
            @Parameter(required = false, name = "cmJobPeoplePhone", description = "检测人员电话"),
    })
    public Result<Boolean> create(@RequestBody DrainPipeNetworkJob job){
        return planService.createJob(job);
    }


    /**
     * 获取计划详情
     * @param cmKid
     * @return
     */
    @Operation(summary = "获取计划详情")
    @PostMapping("/view")
    @Parameters({
            @Parameter(required = true, name = "cmKid", description = "计划id")
    })
    @DictTranslation({
            @FieldParam(dictType = DicConstant.PLAN_PIPE_TYPE, targetField = "cmPlanPipeType")
    })
    public Result<DrainPipeNetworkPlan> view(@RequestParam Long cmKid){
        return planService.view(cmKid);
    }


    /**
     * 获取监测计划列表
     * @param plan
     * @return
     */
    @Operation(summary = "导出监测计划")
    @PostMapping("/export")
    @Parameters({
            @Parameter(required = false, name = "cmPlanPipeType", description = "管网类型"), @Parameter(required = false, name = "cmPlanFinishStatus", description = "完成状态"),
            @Parameter(required = false, name = "cmPlanAuditStatus", description = "审核状态"), @Parameter(required = false, name = "cmPlanNo", description = "项目编号"),
            @Parameter(required = false, name = "cmPlanStartDate", description = "项目周期开始时间"),@Parameter(required = false, name = "cmPlanName", description = "项目名称"),
            @Parameter(required = false, name = "ids", description = "监测计划id，多个使用逗号分隔")
    })
    public void export(@RequestBody DrainPipeNetworkPlan plan) throws IOException {
        planService.export(response, plan);
    }

    /**
     * 查看管网覆盖率
     * @param ids
     * @return
     */
    @Operation(summary = "查看管网覆盖率")
    @GetMapping("/view-coverage")
    @Parameters({
            @Parameter(required = true, name = "ids", description = "计划id，多个使用逗号分隔")
    })
    public Result<String> viewCoverage(@RequestParam("ids") String ids){
        return planService.viewCoverage(ids);
    }


    /**
     * 根据guid查询管道数据
     * @param guid
     * @return
     */
    @Operation(summary = "查看管道信息")
    @GetMapping("/pip-info")
    @Parameters({
            @Parameter(required = true, name = "guid", description = "计划id，多个使用逗号分隔")
    })
    public Result<DrainPipeNetworkPage> pipInfo(@RequestParam("guid") String guid){
        return planService.pipInfo(guid);
    }

}
